View Javadoc
1   package edu.jiangxin.apktoolbox.help.settings;
2   
3   import edu.jiangxin.apktoolbox.swing.extend.EasyChildTabbedPanel;
4   import edu.jiangxin.apktoolbox.utils.Constants;
5   import org.apache.commons.lang3.StringUtils;
6   
7   import javax.swing.*;
8   import java.awt.event.ActionEvent;
9   import java.awt.event.ActionListener;
10  import java.util.Locale;
11  
12  public class LocalePanel extends EasyChildTabbedPanel {
13      private JPanel optionPanel;
14  
15      private JComboBox<String> typeComboBox;
16  
17      private JPanel operationPanel;
18  
19      private static final String[] SUPPORTED_LANGUAGES = {Locale.CHINESE.getLanguage(), Locale.ENGLISH.getLanguage()};
20  
21      @Override
22      public void createUI() {
23          BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
24          setLayout(boxLayout);
25  
26          createOptionPanel();
27          add(optionPanel);
28  
29          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
30  
31          createOperationPanel();
32          add(operationPanel);
33  
34          add(Box.createVerticalStrut(15 * Constants.DEFAULT_Y_BORDER));
35      }
36  
37      private void createOptionPanel() {
38          optionPanel = new JPanel();
39          optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.X_AXIS));
40  
41          JLabel typeLabel = new JLabel("Locale:");
42          typeComboBox = new JComboBox<>();
43  
44          String currentLocaleLanguage = conf.getString("locale.language");
45          if (StringUtils.isEmpty(currentLocaleLanguage)) {
46              currentLocaleLanguage = Locale.ENGLISH.getLanguage();
47              conf.setProperty("locale.language", currentLocaleLanguage);
48          }
49  
50          for (String language : SUPPORTED_LANGUAGES) {
51              typeComboBox.addItem(language);
52              if (StringUtils.equals(currentLocaleLanguage, language)) {
53                  typeComboBox.setSelectedItem(language);
54              }
55          }
56  
57          optionPanel.add(typeLabel);
58          optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
59          optionPanel.add(typeComboBox);
60      }
61  
62      private void createOperationPanel() {
63          operationPanel = new JPanel();
64          operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS));
65  
66          JButton applyButton = new JButton("Apply");
67          applyButton.addActionListener(new ApplyButtonActionListener());
68  
69          operationPanel.add(applyButton);
70      }
71  
72      private final class ApplyButtonActionListener implements ActionListener {
73          @Override
74          public void actionPerformed(ActionEvent e) {
75              String language = (String)typeComboBox.getSelectedItem();
76              if (StringUtils.isNotEmpty(language)) {
77                  conf.setProperty("locale.language", language);
78                  JOptionPane.showMessageDialog(LocalePanel.this, "Setting locale successfully, restart the program please");
79              }
80          }
81      }
82  }